﻿using System;
using Thinker.SignChannel.Times;
using Volo.Abp;
using Volo.Abp.Domain.Entities.Auditing;

namespace Thinker.SignChannel.Entities;

public class Time : AuditedAggregateRoot<Guid>
{
    protected Time()
    {
        Period = string.Empty;
    }

    public Time(Guid id,
        string period,
        Direction direction,
        DateTime signBegin,
        DateTime signTime,
        DateTime signEnd) : base(id)
    {
        var signDate = DateOnly.FromDateTime(signTime);

        SignDate = signDate;
        Period = period;
        Direction = direction;
        ChangeTime(signBegin, signTime, signEnd);
    }

    public DateOnly SignDate { get; private set; }
    public string Period { get; internal set; }
    public Direction Direction { get; internal set; }

    public DateTime SignTime { get; private set; }
    public DateTime SignBegin { get; private set; }
    public DateTime SignEnd { get; private set; }

    public Time ChangeTime(DateTime signBegin,
        DateTime signTime,
        DateTime signEnd)
    {
        Check.Range(signTime.Ticks, nameof(signTime), SignBegin.Ticks + 1L, SignEnd.Ticks - 1L);
        SignTime = signTime;
        SignBegin = signBegin;
        SignEnd = signEnd;
        return this;
    }

    public TimeStatus GetTimeStatus()
    {
        return GetTimeStatus(DateTime.Now);
    }

    public TimeStatus GetTimeStatus(DateTime time)
    {
        if (time < SignBegin)
        {
            return TimeStatus.Future;
        }

        if (time > SignEnd)
        {
            return TimeStatus.Past;
        }

        return TimeStatus.Current;
    }
}